package com.tgy.leetcode;

import java.util.Arrays;

/**
 * @Author: tgy
 * @Date: 12/9/20 11:34 AM
 * <p>
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/
 */
public class _123_买卖股票的最佳时机_III_04 {


    public int maxProfit(int[] prices) {

        if (prices == null || prices.length <= 1) {

            return 0;
        }

        int s1 = Integer.MIN_VALUE;
        int s2 = 0;
        int s3 = Integer.MIN_VALUE;
        int s4 = 0;

        for (int i = 0; i < prices.length; i++) {

            s1 = Math.max(s1, -prices[i]);
            s2 = Math.max(s2, s1 + prices[i]);
            s3 = Math.max(s3, s2 - prices[i]);
            s4 = Math.max(s4, s3 + prices[i]);
        }

        return s4;
    }

}
